﻿using MyWPF.Models.Recipe;
using MyWPF.StaticData;
using MyWPF.Views.Others;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace MyWPF.ViewModel.Subpage.RecipePage_Son
{
    public class VM_MotifyRecipe : BaseViewModel
    {
        public VM_MotifyRecipe()
        {
            #region 事件委托
            _close = new DelegateCommand<object>(_Close);
            _min = new DelegateCommand<object>(_Min);
            _moveWindow = new DelegateCommand<object>(_MoveWindow);
            _motify = new DelegateCommand<object>(_Motify);
            #endregion
        }
        #region 基础：前端事件
        /// <summary>
        /// 关闭窗口
        /// </summary>
        private ICommand _close;
        public ICommand Close
        {
            get { return _close; }
            set
            {
                _close = value;
                OnPropertyChanged("Close");
            }
        }
        private void _Close(object obj)
        {
            StaticModels.motifyRecipe.Close();
        }
        /// <summary>
        /// 最小化窗口
        /// </summary>
        private ICommand _min;
        public ICommand Min
        {
            get { return _min; }
            set
            {
                _min = value;
                OnPropertyChanged("Min");
            }
        }
        private void _Min(object obj)
        {
            StaticModels.motifyRecipe.WindowState = System.Windows.WindowState.Minimized;
        }
        /// <summary>
        /// 移动窗口
        /// </summary>
        private ICommand _moveWindow;
        public ICommand MoveWindow
        {
            get { return _moveWindow; }
            set
            {
                _moveWindow = value;
                OnPropertyChanged("MoveWindow");
            }
        }
        private void _MoveWindow(object obj)
        {
            StaticModels.motifyRecipe.DragMove();
        }

        private void OnPropertyChanged(string v)
        {
            throw new NotImplementedException();
        }
        #endregion

        #region 属性绑定：配方参数
        public RecipeContext NewRecipe
        {
            get { return _newRecipe; }
            set { SetProperty(ref _newRecipe, value); }
        }
        private RecipeContext _newRecipe;

        public bool NameReadOnly
        {
            get { return _nameReadOnly; }
            set { SetProperty(ref _nameReadOnly, value); }
        }
        private bool _nameReadOnly;
        #endregion
        #region 属性绑定：右侧UserControl
        public UserControl RecipeContext
        {
            get { return _recipeContext; }
            set { SetProperty(ref _recipeContext, value); }
        }
        private UserControl _recipeContext;
        #endregion
        #region 属性绑定：按钮Content
        /// <summary>
        /// 按钮Content
        /// </summary>
        public string ButtonContent
        {
            get { return _buttonContent; }
            set { SetProperty(ref _buttonContent, value); }
        }
        private string _buttonContent;
        #endregion

        #region 事件绑定：按钮
        /// <summary>
        /// 按钮新增或修改
        /// </summary>
        private ICommand _motify;
        public ICommand Motify
        {
            get { return _motify; }
            set
            {
                _motify = value;
                OnPropertyChanged("Motify");
            }
        }
        private void _Motify(object obj)
        {
            switch(Function)
            {
                //添加一条配方
                case Func.Add:
                    if (NewRecipe.Name == null || NewRecipe.Name == String.Empty)
                    {
                        Tips tip1 = new Tips("配方名不能为空！");
                        tip1.ShowDialog();
                        break;
                    }
                    #region 给NewRecipe添加属性
                    NewRecipe.CreateTime = DateTime.Now;
                    NewRecipe.Owner = StaticModels.user.User.UserName;
                    NewRecipe.ModifyTime = DateTime.Now;
                    NewRecipe.OwnerPower = StaticModels.user.User.PowerLevel;
                    #endregion
                    if (StaticModels.recipe.AddRecipe(NewRecipe))
                    {
                        StaticModels.motifyRecipe.Close();
                        //添加日志
                        StaticModels.journal.AddJournal(new Models.Journal.JournalContext()
                        {
                            Context = "添加配方'" + NewRecipe.Name + "'",
                            User = StaticModels.user.User.UserName
                        });
                        Tips tip2 = new Tips("添加配方成功！");
                        tip2.ShowDialog();
                        break;
                    }
                    else
                    {
                        Tips tip2 = new Tips("添加配方失败！有可能是因为配方重名。");
                        tip2.ShowDialog();
                        break;
                    }
                    
                //修改一条配方
                case Func.Motify:
                    #region 给NewRecipe修改属性
                    NewRecipe.Owner = StaticModels.user.User.UserName;
                    NewRecipe.ModifyTime = DateTime.Now;
                    #endregion
                    StaticModels.recipe.ModifyRecipe(NewRecipe);
                    StaticModels.motifyRecipe.Close();
                    //添加日志
                    StaticModels.journal.AddJournal(new Models.Journal.JournalContext()
                    {
                        Context = "修改配方'" + NewRecipe.Name + "'",
                        User = StaticModels.user.User.UserName
                    });
                    Tips tip3 = new Tips("修改配方成功！");
                    tip3.ShowDialog();
                    break;
            }

            StaticModels.vM_Recipe.RefreshRecipe();
        }
        public Func Function;

        #endregion
    }

    public enum Func
    {
        Add,
        Motify
    }
}
